/**
 * Copyright (C), 2015-2021, XXX有限公司
 * FileName: MaterialGroupServiceImpl
 * Author:   Administrator
 * Date:     2021/8/4 0004 14:30
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package cn.hxlc.tie.demo.modular.material.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapBuilder;
import cn.hxlc.tie.demo.core.util.NumberUtil;
import cn.hxlc.tie.demo.error.MdmExceptionEnum;
import cn.hxlc.tie.demo.modular.material.entity.Materialgroup;
import cn.hxlc.tie.demo.modular.material.mapper.MaterialgroupMapper;
import cn.hxlc.tie.demo.modular.material.model.param.MaterialgroupParam;
import cn.hxlc.tie.demo.modular.material.model.result.MaterialgroupResult;
import cn.hxlc.tie.demo.modular.material.service.MaterialgroupService;
import cn.hxlc.tie.libs.mp.page.PageFactory;
import cn.hxlc.tie.model.exp.ServiceException;
import cn.hxlc.tie.model.page.PageResult;
import cn.hxlc.tie.system.api.constants.DistTypeConstants;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.hxlc.tie.demo.core.constant.TreeConst.ROOT_ID;
import static cn.hxlc.tie.demo.core.constant.TreeConst.ROOT_PID;

@Service
public class MaterialgroupServiceImpl extends ServiceImpl<MaterialgroupMapper, Materialgroup> implements MaterialgroupService {


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(MaterialgroupParam param){
        NumberUtil.checkRepeatNumber(this.baseMapper,param.getFnumber(),param.getFid());
        Materialgroup entity = getEntity(param);
        this.save(entity);
    }


    @Override
    public List<Map<String, Object>> queryTree() {
        List<Map<String, Object>> treeMaps =
                this.listMaps(new QueryWrapper<Materialgroup>().select("fid as nodeId", "fname as nodeName", "fparentid as nodePid"));

        //装配根节点
        Map<String, Object> rootNode = MapBuilder.create(new HashMap<String, Object>())
                .put("nodeId", ROOT_ID)
                .put("nodeName", "物料分类")
                .put("nodePid", ROOT_PID).build();
        treeMaps.add(rootNode);
        return treeMaps;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long fid){
            this.removeById(fid);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(MaterialgroupParam param){
        NumberUtil.checkRepeatNumber(this.baseMapper,param.getFnumber(),param.getFid());
        Materialgroup oldEntity = getOldEntity(param);
        Materialgroup newEntity = getEntity(param);
        BeanUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }


    @Override
    public PageResult<MaterialgroupResult> findPageBySpec(MaterialgroupParam param){
        Page pageContext = getPageContext();
        IPage<MaterialgroupResult> page = this.baseMapper.customPageList(pageContext, param);
        return new PageResult<>(page);
    }


    private Page getPageContext() {
        return PageFactory.defaultPage();
    }

    private Materialgroup getEntity(MaterialgroupParam param) {
        Materialgroup entity = new Materialgroup();
        BeanUtil.copyProperties(param, entity);
        return entity;
    }

    private Materialgroup getOldEntity(MaterialgroupParam param) {
        return this.getById(getKey(param));
    }


    private Serializable getKey(MaterialgroupParam param){
        return param.getFid();
    }

    @Override
    public Materialgroup detail(Long fid) {
        return this.getById(fid);
    }

}
